# -*- coding: utf-8 -*-
from odoo import models, fields, api, tools, _
from odoo.exceptions import ValidationError, Warning
import datetime


class RiskEvent(models.Model):
    """
    事件名称
    """
    _name = "tyibs.risk.event.category"

    name = fields.Char(string=u"事件名称",required=True)
    code = fields.Char(string=u"事件代码",required=True)

    _sql_constraints = [
        ("unique_code", "unique(code)", u"事件代码已经存在！")
    ]


class RiskCategory(models.Model):
    """
    风险评测对象：电梯品牌，维保单位，使用单位
    """
    _name = "tyibs.risk.category"

    name = fields.Char(string=u"评测对象",required=True)
    code = fields.Char(string=u"对象代码",required=True)
    sub_category_ids = fields.One2many("tyibs.risk.sub.category", "category_id", string=u"评测类型")

    _sql_constraints = [
        ("unique_code", "unique(code)", u"风险评测对象代码已经存在！")
    ]


class RiskSubCategory(models.Model):
    """
    评测类型
    """
    _name = "tyibs.risk.sub.category"

    name = fields.Char(string=u"评测类型",required=True)
    code = fields.Char(string=u"类型代码",required=True)
    weight = fields.Float(string=u"权重比例",required=True, help=u"0到1之间。")
    category_id = fields.Many2one("tyibs.risk.category", string=u"评测对象", required=True)
    event_score_ids = fields.One2many("tyibs.risk.event.score", "sub_category_id", string=u"事件分值配置")

    _sql_constraints = [
        ("unique_category_code", "unique(category_id,code)", u"风险评测对象及类型代码已经存在！")
    ]

class RiskEventScore(models.Model):
    """
    事件分值配置
    """
    _name = "tyibs.risk.event.score"
    _rec_name = "event_category_id"

    event_category_id = fields.Many2one("tyibs.risk.event.category", string=u"事件名称",required=True)
    sub_category_id = fields.Many2one("tyibs.risk.sub.category", string=u"评测类型", required=True)
    code = fields.Char(related="event_category_id.code")
    score = fields.Float(string=u"分值",required=True)
    is_double = fields.Boolean(string=u"多次双倍")


class RiskEventLine(models.Model):
    """
    事件记录
    """
    _name = "tyibs.risk.event.line"
    _rec_name = "event_category_id"
    _order = "id desc"

    date = fields.Date(string=u"时间", default=fields.Datetime.now()[:10])
    event_category_id = fields.Many2one("tyibs.risk.event.category", string=u"事件名称", required=True)
    event_code = fields.Char(related="event_category_id.code", store=True)
    lift_id = fields.Many2one("tyibs.base.lift", string=u"电梯")
    factory_id = fields.Many2one("tyibs.base.lift.factory", string=u"品牌", related="lift_id.factory_id")
    use_company_id = fields.Many2one("tyibs.base.lift.use.company",string=u"使用单位", related="lift_id.use_company_id")
    maintenance_company_id = fields.Many2one("tyibs.base.maintenance.company",string=u"维保单位", related="lift_id.maintenance_company_id")

    res_model = fields.Char(string=u"模型来源")
    res_id = fields.Char(string=u"模型ID")


class RiskEventAddition(models.Model):
    """
    附加事件记录
    """
    _name = "tyibs.risk.event.addition"
    _rec_name = "date"
    _order = "id desc"

    date = fields.Date(string=u"时间", default=fields.Datetime.now()[0:10], required=True)

    factory_id = fields.Many2one("tyibs.base.lift.factory", string=u"电梯品牌")
    use_company_id = fields.Many2one("tyibs.base.lift.use.company",string=u"使用单位")
    maintenance_company_id = fields.Many2one("tyibs.base.maintenance.company",string=u"维保单位")
    addition_score = fields.Float(string=u"扣分数值", required=True)
    create_user_id = fields.Many2one("res.users", default=lambda self: self.env.uid, string=u"扣分人")

    remark = fields.Text(string=u"备注", required=True)

    @api.model
    def create(self, vals):
        if vals.get("factory_id", False) or vals.get("use_company_id", False) or vals.get("maintenance_company_id", False):
            return super(RiskEventAddition, self).create(vals)
        raise ValidationError(u"请至少选择一个扣分对象。")


class RiskBase(models.Model):

    _name = "tyibs.risk.base"
    _order = "risk_create_time desc"

    name = fields.Char(string=u"名称")
    risk_create_time = fields.Datetime(string=u"生成时间")
    # 评测时间默认是上个月15号，方便统计搜索用的：比如生成时间是6月，其实这条记录是针对5月的统计，理论上这条记录只要精确到月，所以默认15号
    risk_date = fields.Date(string=u"打分时间")
    addition_score = fields.Integer(string=u"附加扣分", default=0)
    complex_score = fields.Integer(string=u"综合评分")
    remark = fields.Text(string="备注")

class RiskBrand(models.Model):
    """
    品牌风险评测，品牌即生产厂家
    """
    _inherit = "tyibs.risk.base"
    _name = "tyibs.risk.brand"

    # 这个名称由生成该记录的方法定：比如电梯月：2018三月，年度：2018年...

    save_score = fields.Integer(string=u"故障评分", default=0)
    consumer_score = fields.Integer(string=u"客户评分", default=0)

    factory_id = fields.Many2one("tyibs.base.lift.factory", string=u"电梯制造厂商", required=True)
    province_id = fields.Many2one('tyibs.base.province', related="factory_id.province_id", store=True)
    city_id = fields.Many2one('tyibs.base.province.city', related="factory_id.city_id", store=True)
    area_id = fields.Many2one('tyibs.base.province.city.area', related="factory_id.area_id", store=True)
    address = fields.Char(string=u"详细地址", related="factory_id.address", store=True)
    contact = fields.Char(string=u"联系人", related="factory_id.contact", store=True)
    phone = fields.Char(string=u"固定电话", related="factory_id.phone", store=True)
    mobile = fields.Char(string=u"手机号码", related="factory_id.mobile", store=True)


class RiskMaintenance(models.Model):
    """
    维保单位风险监测
    """
    _inherit = "tyibs.risk.base"
    _name = "tyibs.risk.maintenance.company"

    save_score = fields.Integer(string=u"故障评分", default=0)
    consumer_score = fields.Integer(string=u"客户评分", default=0)
    main_score = fields.Integer(string=u"维保评分", default=0)
    check_score = fields.Integer(string=u"检验评分", default=0)

    maintenance_company_id = fields.Many2one("tyibs.base.maintenance.company", string="维保单位", required=True)
    trust_code = fields.Char(related="maintenance_company_id.trust_code", store=True)
    area_id = fields.Many2one("tyibs.base.province.city.area", related="maintenance_company_id.area_id", store=True)
    address = fields.Char(related="maintenance_company_id.address", store=True)

    #法人
    legal_person = fields.Char(related="maintenance_company_id.legal_person", store=True)
    legal_person_mobile = fields.Char(related="maintenance_company_id.legal_person_mobile", store=True)

    #负责人
    manager_person = fields.Char(related="maintenance_company_id.manager_person", store=True)
    manager_person_mobile = fields.Char(related="maintenance_company_id.manager_person_mobile", store=True)


class RiskUseCompany(models.Model):
    """
    使用单位风险监测
    """
    _inherit = "tyibs.risk.base"
    _name = "tyibs.risk.use.company"

    use_company_id = fields.Many2one("tyibs.base.lift.use.company", string="使用单位", required=True)
    trust_code = fields.Char(related="use_company_id.trust_code", store=True)
    area_id = fields.Many2one("tyibs.base.province.city.area", related="use_company_id.area_id", store=True)
    address_detail = fields.Char(related="use_company_id.address_detail", store=True)

    #法人
    legal_person = fields.Char(related="use_company_id.legal_person", store=True)
    legal_person_mobile = fields.Char(related="use_company_id.legal_person_mobile", store=True)

    #负责人
    manager_person = fields.Char(related="use_company_id.manager_person", store=True)
    manager_person_mobile = fields.Char(related="use_company_id.manager_person_mobile", store=True)


    check_score = fields.Integer(string=u"检验评分", default=0)
    save_score = fields.Integer(string=u"故障评分", default=0)
    consumer_score = fields.Integer(string=u"客户评分", default=0)
    complex_score = fields.Integer(string=u"综合评分",)
